package com.pkk.spring.cloud.service.gateway.selector;

import static com.pkk.spring.cloud.service.gateway.filter.gateway.RequestCustomRateLimiterGatewayFilterFactory.RequestCustomRateLimiterGatewayFilterFactory_BEANNAME;
import static com.pkk.spring.cloud.service.gateway.filter.keyresolver.RequestIpRateLimitKeyResolver.RequestIpRateLimitKeyResolver_BEANNAME;
import static com.pkk.spring.cloud.service.gateway.support.DynamicRouteConfigUtils.GATEWAY_EXT_CLASS_NAME;

import com.pkk.spring.cloud.service.gateway.annotation.GatewayExtAdviceModel;
import com.pkk.spring.cloud.service.gateway.filter.gateway.RequestCustomRateLimiterGatewayFilterFactory;
import com.pkk.spring.cloud.service.gateway.filter.global.CorsFilter;
import com.pkk.spring.cloud.service.gateway.filter.global.GlobalBlacklistFilter;
import com.pkk.spring.cloud.service.gateway.filter.global.GlobalWhitelistFilter;
import com.pkk.spring.cloud.service.gateway.filter.keyresolver.RequestIpRateLimitKeyResolver;
import com.pkk.spring.cloud.service.gateway.global.HttpRequestGlobalFilter;
import com.pkk.spring.cloud.service.gateway.util.BeanRegistrationUtil;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 路由扩展选择器
 *
 * @author peikunkun
 * @version V1.0
 * @date 2021-01-25 17:15
 **/
@Slf4j
public class GatewayExtAdviceModelImportSelector implements ImportBeanDefinitionRegistrar {

  /**
   * 注解key-ext
   */
  private static final String ANNOTATION_KEY_EXT = "ext";


  /**
   * 根据*导入{@code @Configuration}类的给定注释元数据，根据需要注册Bean定义。
   * <p>请注意，由于与{@code @Configuration} *类处理相关的生命周期限制，{@ link
   * BeanDefinitionRegistryPostProcessor}类型可能不会在此处注册。
   * <p>默认实现为空。
   *
   * @param importingClassMetadata 导入类的注释元数据
   * @param 注册表当前bean定义注册表
   */
  @Override
  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    //注册通用的过滤器
    BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, HttpRequestGlobalFilter.class.getName(),
        HttpRequestGlobalFilter.class);

    //获取注解信息
    final AnnotationAttributes annotationAttributes = AnnotationAttributes
        .fromMap(importingClassMetadata.getAnnotationAttributes(GATEWAY_EXT_CLASS_NAME, false));

    //@formatter:off
    //网关支持
    GatewayExtAdviceModel[] exts = (GatewayExtAdviceModel[]) annotationAttributes.get(ANNOTATION_KEY_EXT);
    Arrays.stream(exts).forEach(a -> {
      switch (a) {
        case AUTH:
          //selectRegisterBean();
          log.warn("Sorry, this function is still implemented, if you need to handle it yourself!");
          break;
        case CORS:
          BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, CorsFilter.class.getSimpleName(), CorsFilter.class);
          break;
        case BLACKLIST:
          BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, GlobalBlacklistFilter.class.getSimpleName(),GlobalBlacklistFilter.class);
          break;
        case WHITELIST:
          BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, GlobalWhitelistFilter.class.getSimpleName(),GlobalWhitelistFilter.class);
          break;
        case LIMIT:




          //final BeanDefinition beanDefinition = registry.getBeanDefinition(RequestIpRateLimitKeyResolver_BEANNAME);

          //Map<String, Object> extraPropertyValues=new HashMap<>();
          //extraPropertyValues.put("beanDefinition",beanDefinition);
          BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry,RequestCustomRateLimiterGatewayFilterFactory_BEANNAME ,RequestCustomRateLimiterGatewayFilterFactory.class);
          //请求自定义的ip限制
          BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry,RequestIpRateLimitKeyResolver_BEANNAME ,RequestIpRateLimitKeyResolver.class);
          break;
        case HYSTRIX:
          log.warn("Sorry, this function is still implemented, if you need to handle it yourself!");
          break;
        case NONE:
          break;
        default:
          throw new UnsupportedOperationException("Unsupported gateway operation type " + a);
      }
    });
    //@formatter:on
  }
}
